package com.magicsoft.share.ui.my;

import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.adapter.my.RankAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.my.RankList;
import com.magicsoft.share.bean.my.UserInfo;
import com.magicsoft.share.presenter.my.RecommendPresenter;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.view.my.IRecommendView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import butterknife.BindView;
import butterknife.OnClick;

public class RecommendActivity extends BaseActivity<IRecommendView, RecommendPresenter> implements IRecommendView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.tv_recommend_type)
    TextView tvRecommendType;
    @BindView(R.id.tv_all_num)
    TextView tvAllNum;
    @BindView(R.id.tv_all_money)
    TextView tvAllMoney;
    @BindView(R.id.tv_direct_num)
    TextView tvDirectNum;
    @BindView(R.id.tv_indirect_num)
    TextView tvIndirectNum;
    @BindView(R.id.tv_rank)
    TextView tvRank;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private RankAdapter rankAdapter;


    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isRefresh = true;
    /**
     * 1总排行
     * 2周排行
     */
    private int type = 1;

    @Override
    public RecommendPresenter initPresenter() {
        return new RecommendPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_recommend;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        tvTitle.setText("我的推荐");
        tvRight.setText("邀请说明");


        initRV();

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 0) {
                    //总排行
                    type = 1;
                    pageNo = 1;
                    isRefresh = true;
                    mPresenter.allRanking(pageNo, pageSize);
                } else {
                    //
                    type = 2;
                    pageNo = 1;
                    isRefresh = true;
                    mPresenter.weekRanking(pageNo, pageSize);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }


    @Override
    public void requestData() {
        //
        mPresenter.allRanking(pageNo, pageSize);
        //    mPresenter.weekRankingForUser();
        mPresenter.userDetail();
    }


    @OnClick(R.id.ibtn_left)
    public void onViewClicked() {
        finish();
    }

    private void initRV() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg), 0, DensityUtils.dp2px(this, 0.5f)));       /* recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));*/

        recyclerView.useDefaultLoadMore();

        rankAdapter = new RankAdapter(R.layout.item_rank);
        recyclerView.setAdapter(rankAdapter);

        /**
         *  下拉刷新
         *
         */
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshLayout) {
                isRefresh = false;
                pageNo++;
                Log.i("SLL","TYPE:"+type);
                if (type == 1) {
                    mPresenter.allRanking(pageNo, pageSize);
                } else {
                    mPresenter.weekRanking(pageNo, pageSize);
                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {

            }
        });
    }

    @Override
    public void weekRankingForUserSuccess(String rank) {

    }

    @Override
    public void weekRankingSuccess(RankList data) {
        if (isRefresh) {
            rankAdapter.setNewData(data.getList());
        } else {
            rankAdapter.addData(data.getList());
        }

        int total = data.getList().size();
        if (total <= pageSize) {
            if (total == 0) {
                recyclerView.loadMoreFinish(true, false);
            } else {
                recyclerView.loadMoreFinish(false, false);
            }

        } else {
            recyclerView.loadMoreFinish(false, true);
        }
        smartRefreshLayout.finishLoadmore();
    }

    @Override
    public void allRankingSuccess(RankList data) {

        if (isRefresh) {
            rankAdapter.setNewData(data.getList());
        } else {
            rankAdapter.addData(data.getList());
        }

        int total = data.getList().size();
        if (total <= pageSize) {
            if (total == 0) {
                recyclerView.loadMoreFinish(true, false);
            } else {
                recyclerView.loadMoreFinish(false, false);
            }

        } else {
            recyclerView.loadMoreFinish(false, true);
        }

        smartRefreshLayout.finishLoadmore();
    }

    @Override
    public void rankFail() {
        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }

    @Override
    public void userDetailSuccess(UserInfo userInfo) {
        tvAllNum.setText(String.valueOf(userInfo.getIndirect_num() + userInfo.getDirect_num()));
        tvAllMoney.setText(userInfo.getAccumulated_income() + "");
        tvIndirectNum.setText(userInfo.getIndirect_num() + "");
        tvDirectNum.setText("" + userInfo.getDirect_num());

        if (null == userInfo.getRanking() || "" == userInfo.getRanking()) {

        } else {
            tvRank.setText(userInfo.getRanking());
        }
    }

}
